import requests
from lxml import etree
import os
# 需求：4k图片解析下载（http://pic.netbian.com/4kmeinv/）

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}
url = 'https://pic.netbian.com/4kmeinv/index_{}.html'
if not os.path.exists('getpics'):    # 创建文件夹用于保存图片
    os.mkdir('getpics')
for i in range(1,21):
    page = requests.get(url=url.format(i), headers=headers).text
    page = page.encode('iso-8859-1')
    # page = page.encode('iso-8859-1').decode('gbk')
    # 实例化xpath对象
    tree = etree.HTML(page)
    li_list = tree.xpath('//div[@class="slist"]/ul/li')
    # print(li_list)
    for li in li_list:
        img_src = 'https://pic.netbian.com/' + li.xpath('./a/img/@src')[0]
        img_name = li.xpath('./a/b/text()')[0] + '.jpg'
        # print(img_name, img_src)
        pic_path = 'getpics/' + img_name
        img_data = requests.get(url=img_src, headers=headers).content
        with open(pic_path,'wb') as fp:
            fp.write(img_data)
            print(img_name,'下载成功！!')


